<template>
  <view class="bag-select-page">
    <!-- 页面标题 -->
    <view class="page-header">
      <text class="page-title">申领回收袋</text>
      <text class="page-subtitle">点击回收袋以加入购物车</text>
    </view>

    <!-- 回收袋列表 -->
    <view class="bags-list">
      <view 
        v-for="(bag, index) in bagsList" 
        :key="index"
        class="bag-card"
        @tap="selectBag(bag)"
      >
        <view class="bag-info">
          <view class="bag-details">
            <text class="bag-name">{{ bag.name }}</text>
            <text class="bag-capacity">{{ bag.capacity }}</text>
            <text class="bag-size">{{ bag.size }}</text>
          </view>
          
          <view class="bag-price">
            <text class="price-label">押金:</text>
            <text class="price-amount">¥{{ bag.deposit }}</text>
          </view>
        </view>
        
        <view class="bag-visual">
          <view class="bag-icon" :style="{ background: bag.color }">
            <text class="bag-emoji">{{ bag.emoji }}</text>
            <view class="size-indicator">
              <text class="size-text">{{ bag.sizeLabel }}</text>
            </view>
          </view>
        </view>
      </view>
    </view>

    <!-- 底部操作按钮 -->
    <view class="bottom-action">
      <view class="cart-info">
        <text class="cart-icon">🛒</text>
        <text class="cart-text">购物车</text>
      </view>
      
      <button 
        class="apply-btn"
        :class="{ 'btn-disabled': !selectedBag.id }"
        :disabled="!selectedBag.id"
        @tap="applyBag"
      >
        立即申领
      </button>
    </view>
  </view>
</template>

<script setup>
import { ref } from 'vue'

const selectedBag = ref({})

const bagsList = ref([
  {
    id: 1,
    name: '环保袋（大）',
    capacity: '270L',
    size: '65*55*90cm',
    deposit: '10.00',
    color: 'linear-gradient(135deg, #8BC34A 0%, #7CB342 100%)',
    emoji: '🛍️',
    sizeLabel: '大'
  },
  {
    id: 2,
    name: '环保袋校园版',
    capacity: '182L',
    size: '45*45*90',
    deposit: '10.00',
    color: 'linear-gradient(135deg, #E8F5E8 0%, #F1F8E9 100%)',
    emoji: '🛍️',
    sizeLabel: '小'
  }
])

const selectBag = (bag) => {
  selectedBag.value = bag
  
  // 添加选中效果
  uni.showToast({
    title: `已选择${bag.name}`,
    icon: 'success',
    duration: 1000
  })
}

const applyBag = () => {
  if (!selectedBag.value.id) {
    uni.showToast({
      title: '请先选择回收袋',
      icon: 'none'
    })
    return
  }

  uni.showModal({
    title: '确认申领',
    content: `确定要申领${selectedBag.value.name}吗？押金¥${selectedBag.value.deposit}`,
    success: (res) => {
      if (res.confirm) {
        uni.showLoading({
          title: '申领中...'
        })
        
        setTimeout(() => {
          uni.hideLoading()
          uni.showToast({
            title: '申领成功',
            icon: 'success'
          })
          
          // 通知回收页面更新选中的袋子
          uni.$emit('bagSelected', selectedBag.value)
          
          setTimeout(() => {
            uni.navigateBack()
          }, 1500)
        }, 2000)
      }
    }
  })
}
</script>

<style lang="scss" scoped>
.bag-select-page {
  min-height: 100vh;
  background: #f5f5f5;
  padding: 24rpx 32rpx 200rpx;
}

.page-header {
  text-align: center;
  margin-bottom: 40rpx;
}

.page-title {
  font-size: 36rpx;
  font-weight: 600;
  color: #333;
  display: block;
  margin-bottom: 12rpx;
}

.page-subtitle {
  font-size: 24rpx;
  color: #666;
}

.bags-list {
  margin-bottom: 40rpx;
}

.bag-card {
  background: white;
  border-radius: 20rpx;
  padding: 32rpx;
  margin-bottom: 24rpx;
  display: flex;
  justify-content: space-between;
  align-items: center;
  box-shadow: 0 4rpx 16rpx rgba(0, 0, 0, 0.08);
  transition: all 0.3s ease;
}

.bag-card:active {
  transform: translateY(-2rpx);
  box-shadow: 0 8rpx 24rpx rgba(0, 0, 0, 0.12);
}

.bag-info {
  flex: 1;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.bag-details {
  display: flex;
  flex-direction: column;
}

.bag-name {
  font-size: 32rpx;
  font-weight: 600;
  color: #333;
  margin-bottom: 8rpx;
}

.bag-capacity {
  font-size: 24rpx;
  color: #666;
  margin-bottom: 4rpx;
}

.bag-size {
  font-size: 24rpx;
  color: #999;
}

.bag-price {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
}

.price-label {
  font-size: 22rpx;
  color: #666;
  margin-bottom: 4rpx;
}

.price-amount {
  font-size: 28rpx;
  color: #8BC34A;
  font-weight: 600;
}

.bag-visual {
  margin-left: 32rpx;
}

.bag-icon {
  width: 160rpx;
  height: 120rpx;
  border-radius: 16rpx;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  box-shadow: 0 4rpx 16rpx rgba(0, 0, 0, 0.1);
}

.bag-emoji {
  font-size: 48rpx;
  color: white;
}

.size-indicator {
  position: absolute;
  top: 8rpx;
  right: 8rpx;
  width: 32rpx;
  height: 32rpx;
  background: rgba(255, 255, 255, 0.9);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.size-text {
  font-size: 18rpx;
  color: #8BC34A;
  font-weight: 600;
}

.bottom-action {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  background: white;
  padding: 24rpx 32rpx;
  padding-bottom: calc(24rpx + env(safe-area-inset-bottom));
  box-shadow: 0 -4rpx 20rpx rgba(0, 0, 0, 0.1);
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.cart-info {
  display: flex;
  align-items: center;
  padding: 20rpx;
  background: #f8f8f8;
  border-radius: 50rpx;
}

.cart-icon {
  font-size: 24rpx;
  margin-right: 8rpx;
}

.cart-text {
  font-size: 24rpx;
  color: #666;
}

.apply-btn {
  background: linear-gradient(135deg, #8BC34A 0%, #7CB342 100%);
  color: white;
  border: none;
  border-radius: 50rpx;
  padding: 24rpx 48rpx;
  font-size: 28rpx;
  font-weight: 600;
  box-shadow: 0 8rpx 24rpx rgba(139, 195, 74, 0.3);
  transition: all 0.3s ease;
}

.apply-btn:active {
  transform: translateY(2rpx);
  box-shadow: 0 4rpx 12rpx rgba(139, 195, 74, 0.3);
}

.btn-disabled {
  background: #ccc !important;
  box-shadow: none !important;
}
</style>